package com.weiadmin.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiadmin.dao.UserDao;
import com.weiadmin.model.system.UserModel;
import com.weiadmin.service.UserService;
import com.weiadmin.validate.LoginValidate;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

/**
 * 功能描述
 *
 * @author: shizhongyu
 * @date: 2023年06月01日 10:48
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserModel> implements UserService {
    @Resource
    UserDao dao;

    /**
     * 后台账号密码登录
     * @param session
     * @param request
     * @return
     */
    @Override
    public int doLogin(HttpSession session, LoginValidate request) {
        LambdaQueryWrapper<UserModel> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserModel::getUserLogin,request.getUsername());
        UserModel user = dao.selectOne(lambdaQueryWrapper);
        if(user==null){
            return 0;
        }
        String passwordn = DigestUtil.md5Hex(request.getPassword());
        if(!(passwordn.equals(user.getUserPass()))){
            return 0;
        }
        session.setAttribute("userInfo",user.getUserLogin());
        return 1;
    }


}
